package com.zf.zffencheng.conf;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Date;
/**
 * ClassName: MybatisConfing <br/>
 * Description:  <br/>
 * date: 2021/5/7 10:08<br/>
 *
 * @author william <br/>
 */
@Configuration
@Log4j2
public class MybatisConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Component
    public static class MyMetaObjectHandler implements MetaObjectHandler {

        @Override
        public void insertFill(MetaObject metaObject) {
            log.info("start insert fill ....");
            //自动填充创建时间
            this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
            //自动填充默认不删除
            this.strictInsertFill(metaObject, "delTf", Integer.class, 0);

        }

        @Override
        public void updateFill(MetaObject metaObject) {
            log.info("start update fill ....");
            //自定填充修改时间
            this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        }
    }
}
